
#include <QFuture>
#include <QtConcurrentMap>
#include <QVector>
#include <QPair>
#include <qmath.h>
#include <QDebug>

typedef QPair<double, double> interval_t;
typedef QVector<interval_t> intervals_t;

namespace
{
template<typename Func>
struct MapFunc
{
    typedef double result_type;
    MapFunc(Func& f): _func(f){}

    result_type operator()(const interval_t& interval)
    {
        return (interval.second - interval.first) * (_func(interval.first) + _func(interval.second)) / 2.0;
    }
private:
    Func& _func;
};

template<typename Func>
MapFunc<Func> makeMapFunc(Func& f)
{
    return MapFunc<Func>(f);
}

struct ReduceFunc
{
    typedef double result_type;

    void operator()(result_type& result, const result_type& current)
    {
        result += current;
    }
};
}

int main(int /*argc*/, char** /*argv*/)
{
    intervals_t intervals;

    const double begin = 0;
    const double end = 10;
    const double step = 0.01;

    for (double i = begin; i < end; i += step)
    {
        double width = (end - i < step) ? end - i : step;
        intervals.push_back(qMakePair(i, i + width));
    }

    QFuture<double> result = QtConcurrent::mappedReduced<double>(intervals, makeMapFunc(qSin), ReduceFunc());

    qDebug() << result.result();
}
